package com.rey.demo.api;

import com.rey.demo.link.ZookeeperDemo;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: WatchApi
 * @Description: TODO
 * @Author reyJojo
 * @Date 2021/12/3 12:56
 * @Version 1.0
 */
public class WatchApi {
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = ZookeeperDemo.link4ZookeeperApi();
            // 定义监听器
            Watcher watcher = new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    System.out.println("event type:" + event.getType());
                }
            };
            // 循环监听,重新注册
            while(true) {
                TimeUnit.SECONDS.sleep(1);
                zooKeeper.exists("/app", watcher);
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
